import { getMongoRepository,  } from "typeorm";

import { Classes } from "../models/Classes";

class ClassesService {
  // 根据 id 查找 班级
  findById(id: string) {
    const classesRepository = getMongoRepository(Classes);
    return classesRepository.findOne(id)
  }

  findAll() {
    const classesRepository = getMongoRepository(Classes)
    return classesRepository.find()
  }

  // 根据班级名字模糊查询
  findByClassNameKey(label:string, value: string, skipNum: number, limit: number) {
    const classesRepository = getMongoRepository(Classes);
    return classesRepository.find({
      where: {
        [label]: { $regex: value }
      },
      skip: skipNum,
      take: limit
    })
  }

  // 根据班级名字模糊查询，总数量
  async findByClassNameKeyCount(label:string, value: string) {
    const classesRepository = getMongoRepository(Classes);
    const [users, count] = await classesRepository.findAndCount({
      where: {
        [label]: { $regex: value }
      }
    })
    return count
  }

  // 根据班级名称查询
  findByClassesName(class_name: string) {
    const classesRepository = getMongoRepository(Classes)
    return classesRepository.findOne({class_name})
  }

  // 创建班级
  saveClasses(classes: Classes) {
    const classesRepository = getMongoRepository(Classes)
    return classesRepository.save(classes)
  }

  // 修改班级信息
  updateClasses(id: string, classes: Classes) {
    const classesRepository = getMongoRepository(Classes)
    return classesRepository.update(id, classes)
  }

  // 删除班级
  deleteClassesById(id: string) {
    const classesRepository = getMongoRepository(Classes)
    return classesRepository.delete(id)
  }

}

export default new ClassesService()